﻿using System;
using System.Collections.Generic;
using System.Text;
using MDK;
using System.Drawing;
using System.Windows.Forms;

namespace micro9
{
    class DalizDrawer
    {
        Particle O = new Particle();
        Particle p1 = new Particle();

        Point A = new Point();
        Point B = new Point();
        Point C = new Point();

        double l;

        public DalizDrawer(double Mass, Size s)
        {
            O.m = Mass;
            l = 1.1547 * Mass; // 2 / sqrt(3) * m

            A.X = s.Width - s.Width / 6;
            A.Y = s.Height - s.Height / 6;

            C.X = A.X - (int)l;
            C.Y = A.Y;

            B.X = A.X - (int)l / 2;
            B.Y = A.Y - (int)Mass;
        }

        public void Draw(Graphics g)
        {
            g.DrawPolygon(Pens.Red, new Point[] { A, B, C });
            g.DrawPolygon(Pens.Blue, Points.ToArray());
        }

        List<Point> Points = new List<Point>();

        public void GetFigureBorder(Particle p1, Particle p2, Particle p3, double E1_max)
        {
            Points.Clear();

            while (p1.E < E1_max)
            {
                p1.p = Math.Sqrt(p1.E - p1.m);

                Particle p23 = new Particle(O.m - p1.E);
                p23.p = -p1.p;
                p23.Mass = Math.Sqrt(p23.E * p23.E - p1.p * p1.p);
                

                double E2s = (p23.Mass*p23.Mass + p2.m *p2.m - p3.m * p3.m) / (2 * p23.Mass); // в системе покоя частицы p3
                double P2s = Math.Sqrt(E2s * E2s - p2.m * p2.m);

                double gamma = p23.E / p23.Mass;
                double gammaV = p23.p / p23.Mass;

                double E2_max = gamma * E2s + gammaV * P2s;
                double E2_min = gamma * E2s - gammaV * P2s;

                Points.Add(CountPoint(p1.E, E2_max));
                Points.Add(CountPoint(p1.E, E2_min));

                p1.E++;
            }
        }

        private Point CountPoint(double E1, double E2)
        {/*
            double a = 1 / (B.X - C.X);
            double b = -1 / (B.Y - C.Y);
            double c = - (C.Y / (B.Y - C.Y) + C.X / (B.X - C.X));
            */
            double y = C.Y - E2;
            //double x = (E1 * Math.Sqrt(a * a + b * b) - b * y - c) / a;
            double x = C.X + E2;
            return new Point((int)x, (int)y);
        }
    }
}
